package cn.enzo.mybatis.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.Version;

public class CreateJspFile {

	private static final String filename = "D:\\newFile.jsp";

	public static void main(String[] args) throws Exception {
		HashMap<Object, Object> hashMap = new HashMap<>();
		hashMap.put("name", "hello world");
		Config("1231.ftl",hashMap);
	}

	public static void Config(String templateName,Map<?,?> root) throws Exception {
		
		String templatePath = "D:/";
		Version version = new Version("2.3.9");
		Configuration config = new Configuration(Configuration.VERSION_2_3_19);
		config.setDirectoryForTemplateLoading(new File(templatePath));

		ObjectWrapper objectWrapper = new DefaultObjectWrapper(version);
		config.setObjectWrapper(objectWrapper);
		Template template = config.getTemplate(templateName, "UTF-8");
		FileOutputStream fos = new FileOutputStream(filename);
		
		Writer out = new OutputStreamWriter(fos, "UTF-8");   
        template.process(root, out);   
        out.flush();   
        out.close();   
	}
}
